package com.ocom.company.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ocom.common.entity.company.EntityComDockApi;
import com.ocom.common.vo.company.ComDockApiListVo;
import com.ocom.common.vo.company.GetDockApiConfigVo;
import com.ocom.common.vo.company.GetFeatureVersionVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoComDockApi extends BaseMapper<EntityComDockApi> {

    @Select({"<script>",
            "select \n" +
                    "p.*,a.api_name  \n" +
                    "FROM com_dock_api p \n" +
                    "left join dock_api a on  a.dock_api_code = p.dock_api_code   \n" +
                    "where p.dock_code = #{dockCode} and p.com_id = #{comId}  and p.dock_api_code = #{dockApiCode}  " +
                    " <when test=\"apiPermission!=null  \" > " +
                    " AND  p.api_permission = #{apiPermission} " +
                    " </when> " +
                    "  limit 1 " +
                    "</script>"})
    EntityComDockApi getComDockApi(@Param("dockCode") String dockCode,
                                   @Param("comId") Long comId,
                                   @Param("dockApiCode") String dockApiCode,
                                   @Param("apiPermission") Integer apiPermission);


    @Select({"<script>",
            "select \n" +
                    "p.dock_code , p.dock_api_code, d.dock_name, a.api_name, c.com_appid ,c.server_code , s.server_name   \n" +
                    "FROM com_dock_api p \n" +
                    "left join dock_list d on d.dock_code = p.dock_code   \n" +
                    "left join dock_api a on a.dock_code = p.dock_code  and a.dock_api_code = p.dock_api_code   \n" +
                    "left join company_dock c on c.dock_code = p.dock_code  and c.com_id = #{comId}   \n" +
                    "left join dock_server s on s.dock_code = p.dock_code and s.server_code = c.server_code   \n" +
                    "where p.com_id = #{comId}  and p.api_permission = 2  " +
                    " order by p.dock_code " +
                    "</script>"})
    List<ComDockApiListVo> getComDockApiList(@Param("comId") Long comId);


    /**
     * 从总库平台通讯接口配置1.30com_dock_api获取list
     * select 	p.dock_code as dockCode , p.dock_api_code as dockApiCode , d.dock_name as dockName ,
     * a.api_name as apiName , c.com_appid as comAppid ,
     * c.server_code as serverCode ,  s.server_name as serverName
     * from 	com_dock_api p
     * left join 	dock_list d 	on 	d.dock_code = p.dock_code ,
     * dock_api a 		on 	a.dock_code = p.dock_code  	and 	a.dock_api_code = p.dock_api_code ,
     * company_dock c		on 	c.dock_code = p.dock_code  	and 	c.com_id = @comId ,
     * dock_server s 	on 	s.dock_code = p.dock_code 	and 	s.server_code = c.server_code
     * where 	com_id = @comId 	and 	api_permission = 2
     * order by 	p.dock_code
     */

    @Select({"<script>",
            " SELECT\n" +
                    " a.dock_api_code,\n" +
                    " a.api_name,\n" +
                    " a.api_type,\n" +
                    " c.api_permission,\n" +
                    " c.auto_cycle,\n" +
                    " c.auto_size\n" +
                    " FROM\n" +
                    " dock_api a\n" +
                    " LEFT JOIN com_dock_api c ON c.dock_api_code = a.dock_api_code\n" +
                    " AND c.com_id = #{comId}\n" +
                    " WHERE\n" +
                    " a.dock_code = #{dockCode}\n" +
                    " ORDER BY\n" +
                    " a.dock_api_code" +
                    "</script>"})
    List<GetDockApiConfigVo> getDockApiConfig(@Param("comId") Long comId, @Param("dockCode") String dockCode);

    @Select({"<script>",
            "SELECT " +
                    " a.* " +
                    " FROM " +
                    " com_dock_api a " +
                    " WHERE " +
                    " a.api_permission = 1" +
                    " <when test=\"comId!=null\" > "+
                    " and a.com_id = #{comId}" +
                    " </when>" +
                    " <when test=\"dockCode!=null\" > "+
                    " AND a.dock_code = #{dockCode}" +
                    " </when>" +
                    " <when test=\"dockApiCode!=null\" > "+
                    " AND a.dock_api_code = #{dockApiCode}" +
                    " </when>" +
                    " <when test=\"autoCycle!=null\" > "+
                    " AND a.auto_cycle = #{autoCycle}" +
                    " </when>" +
                    " ORDER BY " +
                    " a.dock_api_code " +
                    "</script>"})
    List<EntityComDockApi> getApiList(@Param("comId") Long comId, @Param("dockCode") String dockCode, @Param("autoCycle") Long autoCycle,@Param("dockApiCode") String dockApiCode);


    @Select({"<script>",
            "SELECT " +
                    " a.api_name,\n" +
                    " u.dock_api_code,\n" +
                    " a.init_api_para,\n" +
                    " a.page_para,\n" +
                    " u.api_para AS reqParam\n" +
                    " FROM\n" +
                    " com_dock_api u\n" +
                    " LEFT JOIN dock_api a ON u.dock_code = a.dock_code\n" +
                    " AND u.dock_api_code = a.dock_api_code\n" +
                    " WHERE\n" +
                    " u.com_id = #{comId}\n" +
                    " AND u.dock_code = 'feature'\n" +
                    " AND u.api_permission &gt; 0 " +
                    "</script>"})
    List<GetFeatureVersionVo> getFeatureVersion(@Param("comId") Long comId);
}
